package com.xuecheng.learning.mq;

import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.learning.config.RabbitMQConfig;
import com.xuecheng.learning.service.CourseLearningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName ChooseCourseTask
 * @Description (监听选课消息，发送选课信息)
 * @date 2020/3/14 15:12
 * @since V1.0
 */
@Component
public class ChooseCourseTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChooseCourseTask.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CourseLearningService courseLearningService;

    /**
     * @Description 监听选课消息
     * @author ShengJinFeng
     * @date 2020/3/14
     */
    @RabbitListener(queues = {RabbitMQConfig.XC_LEARNING_ADDCHOOSECOURSE})
    public void receiveChoosecourseTask(XcTask xcTask) throws IOException {
        //添加选课
        ResponseResult responseResult = courseLearningService.addCourse(xcTask);
        //添加选课成功
        if (responseResult.isSuccess()){
            rabbitTemplate.convertAndSend(RabbitMQConfig.EX_LEARNING_CHOOSECOURSE,RabbitMQConfig.XC_LEARNING_FINISHADDCHOOSECOURSE_KEY,xcTask);
            LOGGER.info("send finish choose course taskId:{}",xcTask.getId());
        }
    }
}
